<?php
class RM_Module_Video extends RM_Module implements RM_Unit_Details_Tray_Interface, RM_Unit_List_Tray_Interface, RM_Unit_Copy_Interface
{
    public function  __construct()
    {
        $this->name = 'Video';
    }

    function copyInformation(RM_Unit_Row $original, RM_Unit_Row $copy)
    {
        $videoModel = new RM_Video();
        $video = $videoModel->fetchByUnit($original->id)->current();
        if ($video == null) {
            return;
        }
        $unitVideo = $videoModel->createRow(array(
            'unit_id' => $copy->id
        ));
        $unitVideoID = $unitVideo->save();

        $unitVideoModel = new RM_UnitVideo();
        $unitVideo = $unitVideoModel->find($video->feed_id)->current();
        $copyVideoData = $unitVideo->toArray();
        unset($copyVideoData['feed_id']);
        $copyVideoData['feed_id'] = $unitVideoID;
        $copyVideo = $unitVideoModel->createRow($copyVideoData);
        $copyVideo->save();
    }

    public function getConfigNode()
    {
        return null;
    }

    public function getNode()
    {
        return null;
    }

    protected function _getHTML(RM_Unit_Row $unit, $iconWidth = 0, $iconHeight = 0)
    {
        $videosModel = new RM_Video();
        $feed = $videosModel->fetchByUnit($unit->id)->current();
        if ($feed == null || $feed->embedcode=="") {
            return "";
        }

        $icon = $this->_getIconURL($feed->provider);
        $iconHTML = "<img src='".$icon."' border='0' ";
        if ($iconWidth !== 0) {
            $iconHTML.= "width='".$iconWidth."px'";
        }
        if ($iconHeight !== 0) {
            $iconHTML.= "height='".$iconHeight."px'";
        }
        $iconHTML.= " >";

        $videoDimensions = $this->_getDimensionsFromEmbedCode($feed->embedcode);
        $url = RM_Environment::getInstance()->getRouter()->_('Video', 'show', array("unit_id" => $unit->id, "page" => "player.phtml"));

        return
        '<a onclick="RM_doShadowBox(\''.$url.'\', \'\', \''.$videoDimensions['width'].'\',\''.$videoDimensions['height'].'\', \'iframe\')">
            '.$iconHTML.'
        </a>';
    }

    protected function _getIconName($provider)
    {
        $currentlySupportedProviders = array(
            'youtube',
            'dailymotion',
            'vimeo'
        );
        if (in_array($provider, $currentlySupportedProviders)) {
            $iconName = $provider.'_icon.gif';
        } else {
            $iconName = 'default_icon.gif';
        }
        return $iconName;
    }

    protected function _getIconURL($provider)
    {
        $iconName = $this->_getIconName($provider);
        return RM_Environment::getConnector()->getRootURL().'RM/userdata/modules/Video/images/'.$iconName;
    }

    public function getHTML(RM_Unit_Row $unit)
    {
        return $this->_getHTML($unit);
    }

    public function getListHTML(RM_Unit_Row $unit)
    {
        return $this->_getHTML($unit, 30, 30);
    }

    private function _getDimensionsFromEmbedCode($embedCode)
    {
        $returnArray = array();

        preg_match('/height=\"[0-9]*\"/',$embedCode, $matches);
        $height = ltrim($matches[0],'height="');
        $height = rtrim($height,'"');
        
        preg_match('/width=\"[0-9]*\"/',$embedCode, $matches);
        $width = ltrim($matches[0],'width="');
        $width = rtrim($width,'"');

        // set default values if no dimensions are returned
        if (!$width || !$height){
            $returnArray['height'] = '640';
            $returnArray['width'] = '385';
        } else {
            $returnArray['height'] = $height;
            $returnArray['width'] = $width;
        }

        return $returnArray;
    }
}